﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Security;
using OnlineManagement.Model;
using OnlineManagement.Web.WebBLL;

namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// Summary description for LoginAndRegist
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LoginAndRegist : System.Web.Services.WebService
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>the username has been existed return 1,otherwise return 0</returns>
        [WebMethod(EnableSession=true)]
        public int IsExsitedUserName(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                UserManagement userManagement = new UserManagement();
                return userManagement.IsExsitedUserName(userName) ? 1 : 0;
            }
            return 2;
        }

        /// <summary>
        /// regist user, regist successfully
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="email"></param>
        /// <param name="pwd"></param>
        /// <param name="pwdConfirm"></param>
        /// <param name="validCode"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public RegisterMsg Regist
            (string userName, 
            string password,
            string hoggeryManager, 
            string hoggeryName, 
            string validCode)
        {
            try
            {
                RegisterMsg msg=new RegisterMsg();
                if (!string.IsNullOrEmpty(userName) 
                    && !string.IsNullOrEmpty(hoggeryManager) 
                    && !string.IsNullOrEmpty(validCode))
                {
                    if (Session["code"] != null)
                    {
                        if (!validCode.Equals(Session["code"].ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            msg = new RegisterMsg
                            {
                                MessageId = RegisterMsg.ErrorMessage.ValidateCode.ToString(),
                                Message = "验证码不正确，请重新输入",
                                Flag=false
                            };
                            return msg;
                        }
                        UserManagement userManagement = new UserManagement();
                        TempRegistUsers user = new TempRegistUsers()
                            {
                                LoginName = userName,
                                Password=password,
                                HoggeryManager = hoggeryManager,
                                HoggeryName = hoggeryName
                            };
                        return msg;
                    }
                    msg = new RegisterMsg { Flag = false, MessageId = RegisterMsg.ErrorMessage.ValidateCode.ToString(), Message = "验证码获取失败，请刷新页面重试。" };
                    return msg;
                }
                return new RegisterMsg() { Flag = false, MessageId = RegisterMsg.ErrorMessage.Unknown.ToString(), Message = "用户注册信息不能为空。" };
            }catch{
                return new RegisterMsg() { Flag=false,MessageId=RegisterMsg.ErrorMessage.Unknown.ToString(),Message="服务器发生错误，注册失败，请稍后重试。"};
            }
        }


    }
}
